package com.example.dllo.my36kr.news.newsecond.third;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.news.NewsBean;
import com.example.dllo.my36kr.news.newsecond.SecondActivity;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.MyApp;
import com.example.dllo.my36kr.tools.NetTool;
import com.example.dllo.my36kr.tools.StaticUrl;

/**
 * Created by dllo on 16/8/25.
 */
public class ThirdActivity extends BaseAty {

    private Button backBnt;
    private TextView showTv;
    private ListView listView;
    private ThirdAdapter adapter;
    private TextView totalCountTv;
    private View view;


    @Override
    protected int setLayout() {
        return R.layout.activity_third;
    }

    @Override
    protected void initView() {
        view = LayoutInflater.from(this).inflate(R.layout.activity_third_head,null);
        backBnt = bindView(R.id.bnt_activity_third_back);//返回
        showTv = bindView(R.id.tv_activity_third_show);
        listView = bindView(R.id.lv_activity_third);
        adapter = new ThirdAdapter(MyApp.getContext());
        totalCountTv = (TextView) view.findViewById(R.id.tv_activity_third_totalCount);//总篇数
        backBnt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

    }

    @Override
    protected void initData() {
        Intent intent =getIntent();
        final String more = intent.getStringExtra("more");//接收值
        final String search = intent.getStringExtra("search");
        mNetTool.getNetData(StaticUrl.SEARCH_URL + more + "&page=1&pageSize=20", ThirdBean.class, new NetTool.NetListener<ThirdBean>() {//网址 拼接
            @Override
            public void onSuccess(ThirdBean thirdBean) {
                adapter.setNewsBean(thirdBean);
                listView.setAdapter(adapter);
                showTv.setText("新闻:" + search);
                totalCountTv.setText("共搜到" + thirdBean.getData().getTotalCount() + "篇文章");
                listView.addHeaderView(view);
            }
            @Override
            public void onError(String errorMsg) {

            }
        });
        mNetTool.getNetData(StaticUrl.SEARCH_URL + more + "&page=1&pageSize=20", NewsBean.class, new NetTool.NetListener<NewsBean>() {
            @Override
            public void onSuccess(final NewsBean newsBean) {
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        if (position !=0) {
                            Intent intent = new Intent(ThirdActivity.this, SecondActivity.class);
                            intent.putExtra("details", newsBean.getData().getData().get(position - 1).getFeedId());
                            startActivity(intent);
                        }else {
                            return;
                        }
                    }
                });
            }

            @Override
            public void onError(String errorMsg) {

            }
        });
    }
}
